//
//  News.m
//  HYHOME
//
//  Created by cjy on 16/2/27.
//  Copyright © 2016年 Mac. All rights reserved.
//

#import "News.h"
#import "NetworkingHandle.h"
#import "Model.h"
#import "NewsCollectionViewCell.h"
#import "UIImageView+WebCache.h"
#import "NetHandleBlock.h"
#import "PushNews.h"
#import "DKNightVersion.h"
#import "MJRefresh.h"
@interface News ()<UICollectionViewDelegate,UICollectionViewDataSource>

@property (nonatomic,retain)UICollectionViewFlowLayout *flowLayout;
@property (nonatomic,retain)UICollectionView *collectionV;
@property (nonatomic,retain)NSMutableArray *array;

@property (nonatomic, assign) NSInteger page;
@property (nonatomic, retain) NSString *strpage;
@property (nonatomic, retain) NSString *str;


@end

@implementation News



-(void)dealloc{
    [_str release];
    [_array release];
    [_flowLayout release];
    [_strpage release];
    [_collectionV release];
    
    [super dealloc];
    


}
- (void)viewDidLoad {
    self.str = [[NSString alloc] init];
    self.array = [NSMutableArray array];


    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.title = @"最新单品";

    
    [self createCollectionView];

    [self handleWithStr:nil];

    
}


- (void)handleWithStr:(NSString *)str {
    if (!str) {
        str = @"http://api.bohejiaju.com/v2/items?gender=2&generation=4&limit=20&offset=0";
    }
    
    
//   self.str = @"http://api.bohejiaju.com/v2/items?gender=2&generation=4&limit=20&offset=0";
    [NetHandleBlock networkingWithURL:str completeHandel:^(NSURLResponse *response, NSData *data, id result) {
        
        
        
        NSDictionary *dicd = [result objectForKey:@"data"];
        NSArray *arrI = [dicd objectForKey:@"items"];
        
        for (NSDictionary *dic in arrI) {
            
            
            NSDictionary *dic2 = [dic objectForKey:@"data"];
            Model *model = [[Model alloc]init];
            
            model.name = [dic2 objectForKey:@"name"];
            model.cover_image_url = [dic2 objectForKey:@"cover_image_url"];
            model.price = [dic2 objectForKey:@"price"];
            model.favorites_count = [dic2 objectForKey:@"favorites_count"];
//
            model.idNumber = [dic2 objectForKey:@"id"];
            [self.array addObject:model];
            [model release];
            
            
        }
        NSDictionary *dicPaging = [dicd objectForKey:@"paging"];
        self.str = [dicPaging objectForKey:@"next_url"];
        
       
        
        [self.collectionV reloadData];
        
    }];

}

- (void)createCollectionView{

    
    [self createFlowLayout];
    
    // 创建对象
    self.collectionV = [[UICollectionView alloc] initWithFrame:[UIScreen mainScreen].bounds collectionViewLayout:self.flowLayout];
    
    [self.view addSubview:self.collectionV];
    
    // delegate
    self.collectionV.dataSource = self;
    self.collectionV.delegate = self;
    
    
    [self.collectionV registerClass:[NewsCollectionViewCell class] forCellWithReuseIdentifier:@"pool1"];
    
    
//    self.collectionV.backgroundColor = [UIColor colorWithRed:245 / 256.f green:245 /256.f blue:255/256.f alpha:1];
    
    self.collectionV.dk_backgroundColorPicker = DKColorWithRGB(0xF5F5FF, 0x343434);

  
    
    self.collectionV.showsVerticalScrollIndicator = NO;
    
    
    
    
    self.collectionV.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            

            [self handleWithStr:self.str];
            [self.collectionV reloadData];
            [self.collectionV.mj_footer endRefreshing];
            
        });
        
        
    }];

    

}
- (void)createFlowLayout{
    
    self.flowLayout = [[UICollectionViewFlowLayout alloc]init];
    self.flowLayout.itemSize = CGSizeMake((self.view.frame.size.width - 30) / 2.0, ((self.view.frame.size.width - 30) / 2.0) * 1.618);
    
    // item 之间最小间距(横向)
//    self.flowLayout.minimumInteritemSpacing = 20;
    // item 之间最小间距(纵向)
    
    
    self.flowLayout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
}

// 两个必须实现的协议方法(1/2)
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
  
    
    return self.array.count;


}


// 两个必须实现的协议方法(2/2)
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    NewsCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"pool1" forIndexPath:indexPath];
    
    // cell 赋值
//    cell.backgroundColor = [UIColor greenColor];
    
    Model *model = [self.array objectAtIndex:indexPath.item];
    
    
    
    [cell passModel:model];
    
    
    return cell;
}


- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{

    PushNews *push = [[PushNews alloc]init];
    
    Model *model = [[Model alloc]init];
    model = self.array[indexPath.item];
    
    push.idNumber = model.idNumber;

    [self.navigationController pushViewController:push animated:YES];
    
    
    [push release];

    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
